home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-05 / pcrte224.zip / SOURCE.ZIP / ETHER.INC < prev    next >
Text File  |  1992-06-09  |  10KB  |  283 lines

  1. ;;*****************************************************************************
  2. ;;                        ether.inc           ether.inc
  3. ;;*****************************************************************************
  4. ;;
  5. ;;  Copyright (C) 1989 Northwestern University, Vance Morrison
  6. ;;
  7. ;;
  8. ;; Permission to view, compile, and modify for LOCAL (intra-organization) 
  9. ;; USE ONLY is hereby granted, provided that this copyright and permission 
  10. ;; notice appear on all copies.  Any other use by permission only.
  11. ;;
  12. ;; Northwestern University makes no representations about the suitability 
  13. ;; of this software for any purpose.  It is provided "as is" without expressed 
  14. ;; or implied warranty.  See the copywrite notice file for complete details.
  15. ;;
  16. ;;*****************************************************************************
  17. ;;
  18. ;; ether.inc constains the board independant, ethernet processing that needs 
  19. ;; to be done.  I rely only on the IF interface defined in if.inc.
  20. ;;
  21. ;; The functions provided by this file are
  22. ;;
  23. ;;   ETH_DECLARE name, interface, task
  24. ;;   ETH_DEFINE name
  25. ;;   ETH_R_READ name, type, code_label
  26. ;;   ETH_R_RETURN name
  27. ;;   ETH_R_CONT_in_BX_CX_ES_const_BX_CX_DX_BP_SI_DI_ES name, ok
  28. ;;   ETH_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP name, no_buffer
  29. ;;   ETH_W_WRITE_in_AX_CX_SI_DI_ES_const_BX_BP_ES name
  30. ;;   ETH_IS_BROADCAST_in_BX_ES_const_AX_BX_CX_DX_BP_DI_ES name
  31. ;;   ETH_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES name
  32. ;;
  33. ;;  Variables Provided by this module (READ ONLY!!!!)
  34. ;;      
  35. ;;      eth_&name&_declared     1 if this module is declared
  36. ;;      eth_&name&_address      The hardware address
  37. ;;      eth_&name&_mtu          The maximum transmission unit
  38. ;;
  39. ;;*****************************************************************************
  40.  
  41.  
  42. ;;*****************************************************************************
  43. ;; defs for ethernet structures
  44.  
  45. IP_TYPE             = 0800h
  46. SWAPPED_IP_TYPE     = 0008h    
  47. ARP_TYPE            = 0806h
  48. SWAPPED_ARP_TYPE    = 0608h     
  49.  
  50. ether   STRUC   
  51.     ether_dst       DB 6 DUP (0)
  52.     ether_src       DB 6 DUP (0)
  53.     ether_type      DW 0
  54. ether   ENDS
  55.  
  56. ;;*****************************************************************************
  57. ;; data storage needed by this module
  58.  
  59. eth_data STRUC
  60.     eth_type_tab    dw 256 dup (0)
  61.     eth_w_try       db ?
  62. eth_data ENDS
  63.  
  64.  
  65. ;;*****************************************************************************
  66. ;;   ETH_DECLARE name, interface, task
  67. ;;       creates a new data link  object.  'name' is the name of this new
  68. ;;       object.  'interface' is the name of the interface to that will provide
  69. ;;       the low level services this module will need.  'task' is the name
  70. ;;       of a task that we need
  71. ;;   
  72. ETH_DECLARE MACRO name, interface, task
  73.     .errb <task>
  74.  
  75.     .DATA
  76.     eth_&name&_declared     = 1
  77.     eth_&name&_interface    = interface
  78.     eth_&name&_task         = task
  79.     eth_&name&_address      = if_&interface&_address
  80.     eth_&name&_mtu          = 1500              ;; maximum transmission unit
  81.  
  82.     global eth_&name&_data:eth_data 
  83.     .CODE
  84.     global eth_&name&_continue:near 
  85.     global eth_&name&_real_define:near
  86. ENDM
  87.  
  88.  
  89. ;;*****************************************************************************
  90. ;;   ETH_DEFINE name
  91. ;;      ETH_DEFINE declare all the things that have to be defined in
  92. ;;      every independantly assembled module.  ETH declares those
  93. ;;      things that need be be done only once (in particular memory allocation
  94. ;;      and initialzation code).  Every module including the one ETH_DEFINE
  95. ;;      is in) must have a ETH_DELCARE.   
  96. ;;
  97. ETH_DEFINE MACRO name
  98.  
  99.     call eth_&name&_real_define
  100. ENDM
  101.  
  102. ETH_REAL_DEFINE MACRO name
  103.     local start, around
  104.     .errb <name>
  105.  
  106. ifdef eth_&name&_declared 
  107.     .DATA
  108.     eth_&name&_data  eth_data <>             ;; create storage needed
  109.  
  110.     .CODE
  111.     jmp around
  112.         start:
  113.         ETH_TASK name
  114.             ;; this does NOT fall through
  115.     around:
  116.     eth_&name&_real_define:
  117.     mov AX, DS                                      ;; initilize table
  118.     mov ES, AX
  119.     mov AX, offset eth_&name&_continue 
  120.     mov DI, offset eth_&name&_data.eth_type_tab
  121.     mov CX, 256
  122.     rep
  123.     stosw
  124.  
  125.     TASK_DEFINE %eth_&name&_task, start          ;; start the task
  126.     RET
  127. endif
  128. ENDM
  129.  
  130.  
  131. ;;******************************************************************************
  132. ;;   ETH_R_READ name, type, code_label
  133. ;;      ETH_R_READ declares that code starting at 'code_label' should be 
  134. ;;      called whenever a packet with type 'type' is read from the IF.  
  135. ;;      The code is called with BX:ES pointing to the start of the packet
  136. ;;      CX loaded with the length of the packet and AX loaded with the 
  137. ;;      type of the packet.  The code should 
  138. ;;      execute ETH_R_RETURN when it is through.
  139. ;;       
  140. ETH_R_READ MACRO name, type, code_label
  141.     local jmp_entry
  142.     .errb <code_label>
  143.  
  144. jmp_entry = eth_&name&_data.eth_type_tab+2*((type / 256) xor (type mod 256))
  145.  
  146.     mov word ptr jmp_entry, offset code_label
  147. ENDM
  148.  
  149. ;;******************************************************************************
  150. ;;   ETH_R_CONT_in_BX_CX_ES name, ok
  151. ;;       ETH_R_CONT determines if the packet returned by R_READ in BX:ES
  152. ;;       of length CX is continuous.  If it is it jumps to 'ok' otherwise
  153. ;;       it just returns
  154. ;;
  155. ETH_R_CONT_in_BX_CX_ES_const_BX_CX_DX_BP_SI_DI_ES MACRO name, ok
  156.     .errb <ok>
  157.  
  158.     IF_R_CONT_in_BX_CX_ES_const_BX_CX_DX_BP_SI_DI_ES %eth_&name&_interface, ok
  159. ENDM
  160.  
  161. ;;******************************************************************************
  162. ;; ETH_R_RETURN 
  163. ;;      ETH_R_RETURN should be called at the end of the read code to signal
  164. ;;      that the read code is finished with the packet read in
  165. ;;
  166. ETH_R_RETURN MACRO name
  167.     .errb <name>
  168.  
  169.     jmp eth_&name&_continue
  170. ENDM
  171.  
  172.  
  173. ;;******************************************************************************
  174. ;;   ETH_W_ACCESS_in_CX_out_DI_ES name, no_buffer
  175. ;;       ETH_W_ACCESS returns a pointer to an output buffer for a (network) 
  176. ;;       packet.  The pointer is returned in DI:ES.  If the output buffer is 
  177. ;;       busy, this routine jump to 'no_buffer'.   The buffer return will
  178. ;;       be at least min(CX, 1500) bytes long
  179. ;;
  180. ETH_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP MACRO name, no_buffer
  181.     .errb <no_buffer>
  182.     
  183.     add CX, size ether
  184.     IF_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP  %eth_&name&_interface, no_buffer
  185.     sub CX, size ether
  186.     add DI, size ether             ;; make it point to the Data part
  187. ENDM
  188.  
  189.  
  190. ;;******************************************************************************
  191. ;;   ETH_W_WRITE_in_AX_CX_SI_DI_ES name, type
  192. ;;       ETH_W_WRITE actually signals the link layer to write a packet to the 
  193. ;;       network.  The packet is assumed to be in the buffer returned by 
  194. ;;       ETH_W_ACCESS.  CX is the length of the packet to send.   SI:DS holds 
  195. ;;       is a pointer to the hardware destination address.  AX is the
  196. ;;       type code of the ethernet packet to send.  
  197. ;;       DI:ES MUST point be the pointer returned by W_ACCESS_out_DI_ES
  198. ;;
  199. ETH_W_WRITE_in_AX_CX_SI_DI_ES_const_BX_BP_ES MACRO name
  200.     local size_ok
  201.     .errb <name>
  202.     
  203.     add CX, size ether
  204.     cmp CX, 60
  205.     jae size_ok
  206.         mov CX, 60
  207.     size_ok:
  208.     sub DI, size ether
  209.     movsw
  210.     movsw
  211.     movsw
  212.  
  213.     mov SI, offset eth_&name&_address
  214.     movsw
  215.     movsw
  216.     movsw
  217.  
  218.     stosw 
  219.     IF_W_WRITE_in_CX_const_BX_BP_ES %eth_&name&_interface
  220. ENDM
  221.  
  222.  
  223.  
  224. ;;******************************************************************************
  225. ;;   ETH_IS_BROADCAST_in_BX_ES name
  226. ;;      ETH_IS_BROADCAST_in_BX_ES determines if the packet pointed to
  227. ;;      by BX:ES is a broadcast and sets the zero flag if it is NOT a 
  228. ;;      broadcast
  229. ;;
  230. ETH_IS_BROADCAST_in_BX_ES_const_AX_BX_CX_DX_BP_DI_ES MACRO name
  231.     .errb <name>
  232.  
  233.     mov SI, BX
  234.     sub SI, size ether
  235.     test byte ptr ES:[SI], 80H
  236. ENDM
  237.  
  238.  
  239. ;;******************************************************************************
  240. ;;   ETH_COPY_in_CX_SI_DI_ES_out_SI_DI interface
  241. ;;   ETH_COPY_in_CX_SI_DI_ES copies a packet from the input buffer (pointed 
  242. ;;      to by SI and the segement register given in IF_DECLARE) to an output 
  243. ;;      buffer (pointed to by DI and dest_reg) of length CX.   It assumes the
  244. ;;      output buffer is contiguous.  (and the caller shouldn't care if the 
  245. ;;      input buffer is contiguous)  COPY updates the pointers SI and DI
  246. ;;      to the end of the packet, and COPY could be called again if CX is not
  247. ;;      the total packet length (Note that CX MUST be even if you care about
  248. ;;      SI, and DI being updated properly)
  249. ;;
  250. ETH_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES MACRO name
  251.     .errb <name>
  252.     IF_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES %eth_&name&_interface
  253. ENDM
  254.  
  255.  
  256. ;;******************************************************************************
  257. ;;   ETH_TASK is the read task that reads the next packet from the interface
  258. ;;   and dispatches it to the next higher protocol layer
  259. ;;
  260. ETH_TASK MACRO name
  261.     local done
  262.     .errb <name>
  263.  
  264.     IF_R_ACCESS_out_BX_CX_ES %eth_&name&_interface, done
  265.     mov AX, word ptr ES:[BX+ether_type]
  266.     add BX, size ether
  267.     sub CX, size ether
  268.  
  269.     xor DX, DX                              ;; compute hash function
  270.     mov DL, AH 
  271.     xor DL, AL
  272.     shl DX, 1
  273.     mov SI, DX                          ;;assume perfect hash function
  274.     jmp [SI+eth_&name&_data.eth_type_tab]
  275.  
  276.     eth_&name&_continue:
  277.         IF_R_FREE_const_BX_CX_BP_SI_DI_ES %eth_&name&_interface
  278.  
  279.     done:
  280.     TASK_RETURN %eth_&name&_task
  281. ENDM
  282.  
  283.